package validarLogin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import negocio.Usuario;
import util.ServletHelper;
import dao.RepositorioUsuario;

/**
 * Servlet implementation class ServletLogin
 */
@WebServlet(name = "MyAnnotationServlet", urlPatterns = { "/login" })
public class ServletLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServletLogin() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println(request.getParameter("inputEmailLogin"));
		System.out.println(request.getParameter("inputSenhaLogin"));
		
		
		RepositorioUsuario rep = new RepositorioUsuario();
		Usuario user  = rep.validarLogin(request.getParameter("inputEmailLogin"),request.getParameter("inputSenhaLogin"));
		
		if(user != null){
			HttpSession session = request.getSession();
			session.setAttribute("usuario", user);
			
			String proximaPagina = "index.jsp";
			ServletHelper.forward(request, response, proximaPagina);
		}else{
			request.setAttribute("mensagem", "Usuario nao logado");		
			String proximaPagina = "home.jsp";
			
			ServletHelper.forward(request, response, proximaPagina);
		}
		
		

	}

}
